<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('分类')" prop="keyReport">
            <el-input v-model="form.keyReport" :placeholder="$t('分类')" show-word-limit :maxlength="100" />
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
          <el-form-item :label="$t('指标Code')" prop="keyCode">
            <el-input v-model="form.keyCode" :placeholder="$t('指标Code')" show-word-limit :maxlength="50" />
          </el-form-item>
        </el-col> -->
        <el-col :span="12">
          <el-form-item :label="$t('指标名称')" prop="keyName">
            <el-input v-model="form.keyName" :placeholder="$t('指标名称')" show-word-limit :maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('公式')" prop="indicatorsFormula">
            <el-autocomplete
              v-model="form.indicatorsFormula"
              class="inline-input"
              :placeholder="$t('公式')"
              readonly
              :trigger-on-focus="false"
              @select="selectGS"
            >
              <template slot="append" @click="selectGS">
                <a @click="selectGS">{{ $t('选择公式') }}</a>
              </template>
            </el-autocomplete>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('重点关注')" prop="focusOn">
            <el-radio-group v-model="form.focusOn" :placeholder="$t('重点关注')">
              <el-radio label="Y">{{ $t('是') }}</el-radio>
              <el-radio label="N">{{ $t('否') }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('数据类型')" prop="keyType">
            <el-radio-group v-model="form.keyType" :placeholder="$t('数据类型')">
              <el-radio label="money">{{ $t('金额') }}</el-radio>
              <el-radio label="ratio">{{ $t('百分比') }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('排序')" prop="sort">
            <el-input v-model="form.sort" :placeholder="$t('排序')" oninput="value=value.replace(/[^\d.]/g,'')" show-word-limit maxlength="20" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <SaveBtn has-permi="company:pcw" />
    </div>
    <!-- 添加或修改关键指标公式（系统模板）对话框 -->
    <el-dialog v-el-drag-dialog :title="$t('选择公式')" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogPcw" append-to-body>
      <pcwTempKeyFormula ref="pcwTempKeyFormula" :company-id="companyId" :formula="formula" :is-sys-temp="true" :type-id="typeId" />
    </el-dialog>
  </div>
</template>

<script>
import { savePcwTempSysKey, getPcwTempSysKey, verifyByKeyName } from '@/api/company/pcw/pcwTempSysKey'
import pcwTempKeyFormula from '@/views/company/pcw/pcwTempKeyFormula.vue'
export default {
  components: {
    pcwTempKeyFormula
  },
  props: {
    typeId: {
      type: String,
      required: true,
      default: ''
    }
  },
  data() {
    // 科目名称唯一校验
    var checkKeyName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('必填信息'))
      } else if (value && this.form.keyReport) {
        const row = {
          id: this.form.id ? this.form.id : null,
          typeId: this.form.typeId,
          keyReport: this.form.keyReport,
          keyName: value
        }
        verifyByKeyName(row).then(res => {
          console.log(res, 'resresres')
          if (res.msg) {
            callback(new Error('已存在该数据'))
          } else {
            callback()
          }
        })
      }
    }
    return {
      // 表单参数
      form: {},
      // 遮罩层
      loading: false,
      // 表单校验
      rules: {
        keyReport: [{ required: true, message: this.$t('必填信息'), trigger: 'change' }],
        keyCode: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        keyName: [{ required: true, validator: checkKeyName, trigger: ['blur', 'change'] }],
        indicatorsFormula: [{ required: true, message: this.$t('必填信息'), trigger: 'change' }]
      },
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      formOpen: false,
      formula: {},
      companyId: ''
    }
  },
  created() {
  },
  methods: {
    // 初始化数据
    init(data) {
      this.loading = true
      if (data != null && data.id !== undefined) {
        getPcwTempSysKey(data.id).then(response => {
          this.form = response.data
          // eslint-disable-next-line no-eval
          this.form.formulaList = eval(this.form.formulaList)
          this.loading = false
        })
      } else {
        this.reset()
        this.loading = false
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        typeId: this.typeId,
        keyCode: undefined,
        keyName: undefined,
        keyReport: undefined,
        indicatorsFormula: undefined,
        indicatorsCodeFormula: undefined,
        sort: undefined,
        keyType: 'money',
        focusOn: 'N',
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined,
        remark: undefined
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      this.$parent.$parent.formOpen = false
      // 刷新list页面
      this.$parent.$parent.getList()
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          this.form.formulaList = JSON.stringify(this.form.formulaList)
          savePcwTempSysKey(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess(this.$t('保存成功'))
              this.closeForm()
            } else {
              this.msgError(response.msg)
            }
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          this.locationError(document)
        }
      })
    },
    /** 选择公式操作 */
    selectGS() {
      this.formOpen = true
      this.formula = {
        name: this.form.indicatorsFormula,
        code: this.form.indicatorsCodeFormula,
        formulaList: this.form.formulaList || []
      }
    },
    addFormula(name, code, formulaList) {
      this.form.indicatorsFormula = name
      this.form.indicatorsCodeFormula = code
      this.form.formulaList = formulaList
    }
  }
}
</script>
<style scoped>

</style>
